package com.sea.supply.controller;

import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import com.codecrane.core.web.AjaxTableReturn;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sea.supply.entity.Produce;
import com.sea.supply.entity.ProduceNode;
import com.sea.supply.service.ProduceNodeService;
import com.sea.supply.service.ProduceService;
import com.sea.supply.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "/sea/supply/produce/node")
public class ProduceNodeController {

    @Autowired
    private ProduceService produceService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private ProduceNodeService produceNodeService;
    /**
     * 管理主页面
     * @param model
     * @return
     */
    @RequestMapping("/main")
    public String main(Model model) {

        return "/sea/supply/produceNode/main";
    }

    @RequestMapping("/editPage")
    public String editPage(Model model,@RequestParam("id")Integer id) {

        model.addAttribute("id",id);
        model.addAttribute("produceNode",produceNodeService.findById(id));
        return "/sea/supply/produceNode/edit";
    }

    @RequestMapping("/editRealPage")
    public String editRealPage(Model model,@RequestParam("id")Integer id) {

        model.addAttribute("id",id);
        model.addAttribute("produceNode",produceNodeService.findById(id));
        return "/sea/supply/produceNode/editReal";
    }

    /**
     * 分页条件查询
     *
     * @param pagesize
     * @param start
     * @return
     */
    @ResponseBody
    @RequestMapping("/findByCnd")
    public AjaxTableReturn findByCnd(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                     @RequestParam(value = "offset", defaultValue = "0") int start,
                                     ProduceNode produceNode) {
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<ProduceNode> list = produceNodeService.findByCnd(produceNode);
        PageInfo<ProduceNode> page = new PageInfo<>(list);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }

    /**
     * 修改
     * @param produceNode
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/usave", method = RequestMethod.POST)
    public AjaxReturn usave(ProduceNode produceNode) {
        AjaxReturn result = Ajax.fail().setMsg("修改失败！");
        try{
            if (null != produceNode && produceNodeService.usave(produceNode) > 0) {
                result.setOk(true).setMsg("修改成功！");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }

    /**
     * 修改
     * @param produceNode
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public AjaxReturn delete(Integer id) {
        AjaxReturn result = Ajax.fail().setMsg("删除失败！");
        try{
            if (null != id && produceNodeService.delete(id) > 0) {
                result.setOk(true).setMsg("删除成功！");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }

}
